package com.pansoft.openplanet.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.efounder.chat.activity.BaseActivity;
import com.pansoft.hd.HDWalletUtil;
import com.pansoft.hd.model.BtcHdWallet;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.adapter.AssetWalletAddressListAdapter;
import com.pansoft.openplanet.bean.Account;
import com.pansoft.openplanet.db.AccountDao;
import com.pansoft.openplanet.util.BtcNetParamUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 钱包子地址列表
 * @author JamesZhang
 */
public class AssetWalletAddressListActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTvRefresh,mTvAddAddress;
    private RecyclerView recyclerView;
    private LinearLayout mLlAddaddress;
    private AssetWalletAddressListAdapter addressListAdapter;
    private List<BtcHdWallet.BtcChildAddress> childAddresses = new ArrayList<>();
    private Account account;
    private AccountDao accountDao = new AccountDao(this);

    public static void start(Context context, Account account) {
        Intent starter = new Intent(context, AssetWalletAddressListActivity.class);
        starter.putExtra("account",account);
        context.startActivity(starter);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_asset_wallet_address_list);
        account = (Account) getIntent().getSerializableExtra("account");
        initView();

    }

    private void initView() {
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(R.string.star_asset_wallet_address);
        mTvRefresh = (TextView) findViewById(R.id.tv_refresh);
        mTvAddAddress = (TextView) findViewById(R.id.tv_add_address);
        mLlAddaddress = (LinearLayout) findViewById(R.id.ll_add_address);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mLlAddaddress.setOnClickListener(this);
        mTvRefresh.setOnClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        addressListAdapter = new AssetWalletAddressListAdapter(childAddresses);
        recyclerView.setAdapter(addressListAdapter);

       initData();
       initListener();
    }

    private void initListener() {

        addressListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                for(BtcHdWallet.BtcChildAddress childAddress : childAddresses){
                    childAddress.setSelected(false);
                }
                BtcHdWallet.BtcChildAddress childAddress = childAddresses.get(position);
                childAddress.setSelected(true);
                //更新Account数据库中当前的子地址
                account.setHdBtcCurrentChildAddress(childAddress.getAddress());
                accountDao.insertOrReplace(account);
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void initData() {
        childAddresses.clear();
        //生成HD钱包子地址
        int hdMaxChildIndex = Integer.parseInt(account.getHdBtcMaxChildIndex());
        showLoading(R.string.common_text_please_wait);
        for(int i = 0;i<hdMaxChildIndex;i++){
            BtcHdWallet.BtcChildAddress childAddress= HDWalletUtil.newReceiveAddress(BtcNetParamUtil.getBtcBipPath(),
                    account.getHdBtcXpub(),i,HDWalletUtil.getNetworkParameters());
            if(childAddress.getAddress().equals(account.getHdBtcCurrentChildAddress())){
                childAddress.setSelected(true);
            }
            childAddress.setIndex(i);
            childAddresses.add(childAddress);
        }
        Collections.reverse(childAddresses);
        addressListAdapter.notifyDataSetChanged();
        dismissLoading();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id == R.id.ll_add_address){
            //添加子地址
            showLoading(R.string.common_text_please_wait);
            int hdMaxChildIndex = Integer.parseInt(account.getHdBtcMaxChildIndex());
            BtcHdWallet.BtcChildAddress childAddress  = HDWalletUtil.newReceiveAddress(BtcNetParamUtil.getBtcBipPath(),
                    account.getHdBtcXpub(),hdMaxChildIndex,HDWalletUtil.getNetworkParameters());
            //设置当前子地址下标
            childAddress.setIndex(hdMaxChildIndex);
            addressListAdapter.addData(0,childAddress);
            //最大子地址数量加一
            hdMaxChildIndex++;
            //保存数据库
            account.setHdBtcMaxChildIndex(hdMaxChildIndex+"");
            accountDao.insertOrReplace(account);
            dismissLoading();
            recyclerView.scrollToPosition(0);
        }else if(id == R.id.tv_refresh){
            //刷新发现地址
            initData();
        }
    }
}
